﻿#include "ConfigDialog.h"
#include "NewPCIDemoDefine.h"
#include "ConfigData.h"
#include <QFileDialog>
#include <QMessageBox>
#include "ApMainWindow.h"

//使用宏代替一些重复性代码
#define defSetFloat(varObj,varName) varObj.varName = ui->lineEdit_##varName->text().toFloat()
#define defSetInt(varObj,varName) varObj.varName = ui->lineEdit_##varName->text().toInt()
#define defSetString(varObj,varName) varObj.varName = ui->lineEdit_##varName->text()
#define defSetEnumInt(varObj,varName) varObj.varName = ui->comboBox_##varName->currentIndex()

#define defLoadNum(varObj,varName) ui->lineEdit_##varName->setText(QString::number(varObj.varName))
#define defLoadString(varObj,varName) ui->lineEdit_##varName->setText(varObj.varName)
#define defLoadEnumInt(varObj,varName) ui->comboBox_##varName->setCurrentIndex(varObj.varName)

ConfigDialog::ConfigDialog(QWidget *parent)
	: QDialog(parent)
	, ui(new Ui::ConfigDialogClass())
{
	ui->setupUi(this);
	initData();
	ApMainWindow *mainWindow = (ApMainWindow *)parent;
	connect(this, &ConfigDialog::pciParamChanged, mainWindow, &ApMainWindow::pciParamChanged);
}

ConfigDialog::~ConfigDialog()
{
	delete ui;
}

void ConfigDialog::initData()
{
	loadPCIParam();
	loadHardwareParam();
}

void ConfigDialog::on_pushButton_config_clicked()
{
	savePCIParam();
	ui->label_10->setText("已设置");
	emit pciParamChanged();
}

void ConfigDialog::on_pushButton_config_2_clicked()
{
	saveHardwareParam();
	ui->label_12->setText("已设置");
}

void ConfigDialog::on_toolButton_clicked()
{
	QString oldPath = ui->lineEdit_xmlPath->text();
	QString dir = QFileDialog::getOpenFileName(this, "选择原始数据保存路径", oldPath, "XML Files (*.xml)");
	if (!dir.isEmpty()) {
		ui->lineEdit_xmlPath->setText(dir);
	}
}

void ConfigDialog::savePCIParam()
{
	PtrConfig->xmlPath = ui->lineEdit_xmlPath->text();

	As::getConfig()->save();
	QMessageBox::warning(this, "提示", "配置保存成功");
	//accept();
	qDebug() << PtrConfig->xmlPath;
}

void ConfigDialog::loadPCIParam()
{
	//defLoadString(PtrConfig->xmlPath, xmlPath);
	ui->lineEdit_xmlPath->setText(PtrConfig->xmlPath);
}

void ConfigDialog::saveHardwareParam()
{
	defSetFloat(PtrConfig->paramHardware, expandValue);
	defSetInt(PtrConfig->paramHardware, imgAccount);
	defSetEnumInt(PtrConfig->paramHardware, processWay);
	defSetFloat(PtrConfig->paramHardware, rowFrequency);
	defSetFloat(PtrConfig->paramHardware, colFrequency);

	As::getConfig()->save();
	QMessageBox::warning(this, "提示", "配置保存成功");
	//accept();
}

void ConfigDialog::loadHardwareParam()
{
	defLoadNum(PtrConfig->paramHardware, expandValue);
	defLoadNum(PtrConfig->paramHardware, imgAccount);
	defLoadEnumInt(PtrConfig->paramHardware, processWay);
	defLoadNum(PtrConfig->paramHardware, rowFrequency);
	defLoadNum(PtrConfig->paramHardware, colFrequency);
}
